package cifrasong.cifra.gui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import cifrasong.R;
import cifrasong.cifra.dominio.Cifra;
import cifrasong.usuario.dominio.Session;
import cifrasong.usuario.gui.MenuPrincipalAct;

/**
 * Created by Uehara on 01/12/2014.
 */

public class FavoritoAct extends Activity {

    List<Cifra> cifras;
    ArrayAdapter cifrasAdapter;
    ListView lista;

    public void onBackPressed(){
        Intent intent = new Intent();
        intent.setClass(this, MenuPrincipalAct.class);
        startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lista_favoritos);

        cifras = Session.getUsuarioLogado().getListaFavorita();
        cifrasAdapter = new CifrasAdapter(this);
        lista = (ListView) findViewById(R.id.minhasCifras);
        lista.setAdapter(cifrasAdapter);
        lista.setClickable(true);

        final TextView info = (TextView)findViewById(R.id.info);
        if (Session.getUsuarioLogado().getListaFavorita().size()<= 0){
            info.setVisibility(TextView.VISIBLE);
        }

        lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Cifra cifraSelecionada = (Cifra) lista.getItemAtPosition(i);
                Session.setCifraSelecionada(cifraSelecionada);

                Intent intent = new Intent(FavoritoAct.this, ExibeCifraFavoritaAct.class);
                startActivity(intent);
            }
        });
    }


    private class CifrasAdapter extends ArrayAdapter<Cifra>{
        private Context ctx;

        public CifrasAdapter(Context ctx){
            super(ctx, R.layout.activity_lista_favoritos, cifras);
            this.ctx = ctx;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent){

            if (view == null){
                view = getLayoutInflater().from(this.ctx).inflate(R.layout.itens_list_view, null);
            }

            Cifra cifra = cifras.get(position);

            TextView nomeCifra = (TextView) view.findViewById(R.id.musicaCifra);
            TextView artistaCifra = (TextView) view.findViewById(R.id.artistaCifra);
            nomeCifra.setText(cifra.getNome());
            artistaCifra.setText(cifra.getArtista());

            return view;
        }

    }
}